package net.minecraft.src;
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode 

import java.util.List;
import java.util.Random;
import org.lwjgl.opengl.GL11;

public class EntityTownArcher extends EntityCreature
{
    public void onUpdate()
    {
        moveSpeed = playerToAttack == null ? 0.1F : 0.3F;
        if(randomSoundDelay > 0 && --randomSoundDelay == 0)
        {
        }
        super.onUpdate();
		arrows += 0.1;
    }

    public EntityTownArcher(World world)
    {
        super(world);
        angerLevel = 0;
        randomSoundDelay = 0;
        texture = "/mob/towngaurd.png";
        moveSpeed = 0.1F;
		health = 8;
        isImmuneToFire = true;
		arrows = 50;
    }

    protected String getHurtSound()
    {
        return "random.hurt";
    }

    protected String getDeathSound()
    {
        return "random.hurt";
    }

    public void onLivingUpdate()
    {
        super.onLivingUpdate();
    }

    protected void attackEntity(Entity entity, float f)
    {
		if(arrows > 0)
		{
			if(f < 10F)
			{
				double d = entity.posX - posX;
				double d1 = entity.posZ - posZ;
				if(attackTime == 0)
				{
					EntityArrow entityarrow = new EntityArrow(worldObj, this);
					entityarrow.posY += 1.3999999761581421D;
					double d2 = entity.posY - 0.20000000298023224D - entityarrow.posY;
					float f1 = MathHelper.sqrt_double(d * d + d1 * d1) * 0.2F;
					worldObj.playSoundAtEntity(this, "random.bow", 1.0F, 1.0F / (rand.nextFloat() * 0.4F + 0.8F));
					worldObj.entityJoinedWorld(entityarrow);
					entityarrow.setArrowHeading(d, d2 + (double)f1, d1, 0.6F, 12F);
					attackTime = 10;
					arrows -= 1;
				}
				rotationYaw = (float)((Math.atan2(d1, d) * 180D) / 3.1415927410125732D) - 90F;
				hasAttacked = true;
				
			}
		}
		else         
		if((double)f < 2.5D && entity.boundingBox.maxY > boundingBox.minY && entity.boundingBox.minY < boundingBox.maxY)
        {
            attackTime = 40;
            entity.attackEntityFrom(this, 4);
        }
    }
	
	public void becomeAngryAt(Entity entity)
    {
        playerToAttack = entity;
        angerLevel = 400 + rand.nextInt(400);
        randomSoundDelay = rand.nextInt(40);
    }
	
	    protected Entity findPlayerToAttack()
    {
        if(angerLevel == 0)
        {
            return null;
        } else
        {
            return super.findPlayerToAttack();
        }
    }

	
	public boolean attackEntityFrom(Entity entity, int i)
    {
        if(entity instanceof EntityPlayer)
        {
            List list = worldObj.getEntitiesWithinAABBExcludingEntity(this, boundingBox.expand(32D, 32D, 32D));
            for(int j = 0; j < list.size(); j++)
            {
                Entity entity1 = (Entity)list.get(j);
                if(entity1 instanceof EntityTownArcher)
                {
                    EntityTownArcher entitytownarcher = (EntityTownArcher)entity1;
                    entitytownarcher.becomeAngryAt(entity);
                }
            }

            becomeAngryAt(entity);
        }
        return super.attackEntityFrom(entity, i);
    }

    public void writeEntityToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeEntityToNBT(nbttagcompound);
		nbttagcompound.setShort("Anger", (short)angerLevel);
    }

    public void readEntityFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readEntityFromNBT(nbttagcompound);
		angerLevel = nbttagcompound.getShort("Anger");
    }

	/*
    protected int getDropItemId()
    {

    }
	*/
	
	public void onDeath(Entity entity)
	{
	dropItem(Item.arrow.shiftedIndex,arrows/8+1);
	}
	
    public ItemStack getHeldItem()
    {
	if(arrows > 0)
		{
        return defaultHeldItem;
		}
	else
		{
		return defaultHeldItem2;
		}
    }

    private static final ItemStack defaultHeldItem;
	private static final ItemStack defaultHeldItem2;
	private int angerLevel;
    private int randomSoundDelay;
	private int arrows;

    static 
    {
        defaultHeldItem = new ItemStack(Item.bow, 1);
		defaultHeldItem2 = new ItemStack(268, 1);
    }
}
